package db

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/mattn/go-sqlite3"
	. "zhhuiyan/models"
)

var Handler  orm.Ormer

func init() {
	orm.RegisterDataBase("default", "sqlite3", "./data.db")
	// 数据库别名
	name := "default"

	// drop table 后再建表
	force := true

	// 打印执行过程
	verbose := true

	// 遇到错误立即返回
	err := orm.RunSyncdb(name, force, verbose)
	if err != nil {
		fmt.Println(err)
	}
	Handler = orm.NewOrm()
}

func List(model string, page *Page, models interface{}) ( error) {
	page.Records, _ = Handler.QueryTable(model).Count()
	_, err := Handler.QueryTable(model).Limit(20, 0).RelatedSel().All(models)
	page.Pages = page.Records/page.Size+1
	return err
}



